package com.team.abs.controller;

import java.security.GeneralSecurityException;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import javax.mail.MessagingException;

import org.apache.tomcat.jni.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.team.abs.entity.man_time;
import com.team.abs.entity.simple_user;
import com.team.abs.entity.user_item;
import com.team.abs.service.user_status_service;

import com.google.common.cache.LoadingCache;
import com.team.abs.service.SendEmail;
import com.team.abs.service.cache;
import net.sf.json.JSONObject;
@Controller
public class user_status_controller {
	@Autowired
	private user_status_service user_status_service_1;
	//使用 HASHMAP 和优先队列 建立一个有效时间功能的邮箱验证功能
	private static HashMap<String,String>map=new HashMap();
	private static PriorityQueue <man_time>pri=new PriorityQueue(
			new Comparator<man_time>() {
				public int compare(man_time e1,man_time e2 ) {
					return (int)(e1.getTime()-e2.getTime());
				}
			}
	);
	//查询显示用户信息
	@ResponseBody
	@RequestMapping(value="/manSearch_api",method=RequestMethod.POST)
	public String manInsert(
			@RequestParam(value="user_uid",required=true)String user_uid){
		JSONObject result=new JSONObject();
		simple_user user=user_status_service_1.search_by_uid(user_uid);
		result.put("result", user);
		//System.out.println(user.toString());
		result.put("status", "success");
		return result.toString();
	} 
	//修改用户信息
	@ResponseBody
	@RequestMapping(value="/manChange_api",method=RequestMethod.POST)
	public String manChange(
			@RequestParam(value="user_uid",required=true)String user_uid,
			@RequestParam(value="user_qq",required=true)String user_qq,
			@RequestParam(value="user_tel",required=true)String user_tel,
			@RequestParam(value="user_message",required=true)String user_message
			){
		JSONObject result=new JSONObject();
		if(user_status_service_1.updata_qq_tel_message(user_uid, user_qq, user_tel, user_message)>0)
		result.put("status", "success");
		return result.toString();
	} 
	//查询登录者相关的物品信息
	@ResponseBody
	@RequestMapping(value="/search_user_item_api",method=RequestMethod.POST)
	public String search_user_item(
			@RequestParam(value="item_id",required=true)int item_id
			
			){
		JSONObject result=new JSONObject();
		user_item user_item;
		if((user_item=user_status_service_1.find_user_item(item_id))!=null) {
			
		result.put("status", "联系QQ为"+user_item.getUser_qq()+"\n"
				+"联系电话为"+user_item.getUser_tel()+"\n"
				+"与卖家建立联系");
		}
		return result.toString();
	} 
	//修改物品信息
	@ResponseBody
	@RequestMapping(value="/change_item_api",method=RequestMethod.POST)
	public String change_item(
			@RequestParam(value="item_id",required=true)int item_id,
			@RequestParam(value="from_item_status",defaultValue = "0",required=true)int from_item_status,
			@RequestParam(value="form_item_price",defaultValue = "0",required=true)int form_item_price		
			){
		JSONObject result=new JSONObject();
		if(user_status_service_1.update_item(item_id, from_item_status, form_item_price)>0)
			result.put("status","success");
		return result.toString();
	} 
	//删除物品信息
	@ResponseBody
	@RequestMapping(value="/delete_item_api",method=RequestMethod.POST)
	public String delete_item(
			@RequestParam(value="item_id",required=true)int item_id,
			@RequestParam(value="user_uid",required=true)String user_uid
			
			){
		JSONObject result=new JSONObject();
		if(user_status_service_1.delete_item(user_uid, item_id)>0)
			result.put("status","成功");
		return result.toString();
	} 
	//进行请求邮箱的发送
	@ResponseBody
	@RequestMapping(value="/get_verification_api",method=RequestMethod.POST)
	public String get_verification(
			@RequestParam(value="user_uid",required=true)String user_uid,
			@RequestParam(value="qq",required=true)String qq) throws GeneralSecurityException, MessagingException{
		//队列的清除
		while(pri.size()>0) {
			if(Time.now()-pri.peek().getTime()>=300) {
				//300S就POLL
				String user=pri.peek().getUid();
				pri.poll();
				map.remove(user);
			}else break;
		}
		
		JSONObject result=new JSONObject();
		String content=String.valueOf(new Random().nextInt(9000)+1000);
		//发信息咯
		SendEmail.send("3184325590@qq.com", qq+"@qq.com", content);
		pri.add(new man_time(user_uid,new Date().getTime()));
		map.put(user_uid, content);
		result.put("status", "success");
		return result.toString();
	} 
	//密码重置请求
	@ResponseBody
	@RequestMapping(value="/change_pass_api",method=RequestMethod.POST)
	public String change_pass(
			@RequestParam(value="user_uid",required=true)String user_uid,
			@RequestParam(value="change_pass",required=true)String change_pass,
			@RequestParam(value="change_pass_after",required=true)String change_pass_after,
			@RequestParam(value="verification",required=true)String verification
			) throws ExecutionException{
		JSONObject result=new JSONObject();
		if(!map.getOrDefault(user_uid,"****").equals(verification)) {
			//System.out.println(map.getOrDefault(user_uid, "123")+" "+verification);
			result.put("status", "邮箱验证失败");
			return result.toString();}
		if(!user_status_service_1.search_by_uid(user_uid).getUser_password().equals(change_pass)) {
			//System.out.println(change_pass+"***"+user_status_service_1.search_by_uid(user_uid).getUser_password());
			result.put("status", "原密码错误");
			return result.toString();
		}
		if(user_status_service_1.update_password(user_uid, change_pass_after)>0){
			map.remove(user_uid);
		result.put("status", "success");
		}else result.put("status", "fail");
		
		return result.toString();
	} 
	
}
